package com.alander.nexusgo.user.controller;

import com.alander.nexusgo.common.response.Result;
import com.alander.nexusgo.framework.web.starter.controller.BaseController;
import com.alander.nexusgo.user.model.entity.UserFollows;
import com.alander.nexusgo.user.service.UserFollowsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户关注、粉丝管理
 */
@RestController
@RequestMapping("/api/user/follows")
@RequiredArgsConstructor
public class UserFollowsController extends BaseController {

    private final UserFollowsService userFollowsService;

    /**
     * 关注用户
     * @param followedId 被关注用户ID
     * @return 关注结果
     */
    @PostMapping("/follow/{followedId}")
    public Result<Boolean> followUser(@PathVariable(value = "followedId") Long followedId) {
        Long followerId = getUserId(); // 从token中获取当前用户ID
        if (followerId.equals(followedId)) {
            return fail("不能关注自己");
        }

        boolean result = userFollowsService.followUser(followerId, followedId);
        return result ? success(true) : fail("关注失败");
    }

    /**
     * 取消关注
     * @param followedId 被关注用户ID
     * @return 取消关注结果
     */
    @PostMapping("/unfollow/{followedId}")
    public Result<Boolean> unfollowUser(@PathVariable(value = "followedId") Long followedId) {
        Long followerId = getUserId(); // 从token中获取当前用户ID
        boolean result = userFollowsService.unfollowUser(followerId, followedId);
        return result ? success(true) : fail("取消关注失败");
    }

    /**
     * 检查是否已关注
     * @param followedId 被关注用户ID
     * @return 是否已关注
     */
    @GetMapping("/check/{followedId}")
    public Result<Boolean> isFollowing(@PathVariable(value = "followedId") Long followedId) {
        Long followerId = getUserId(); // 从token中获取当前用户ID
        boolean result = userFollowsService.isFollowing(followerId, followedId);
        return success(result);
    }

    /**
     * 获取关注列表
     * @return 关注列表
     */
    @GetMapping("/following")
    public Result<List<Long>> getFollowingList() {
        Long userId = getUserId();
        List<Long> followingList = userFollowsService.getFollowingList(userId);
        return success(followingList);
    }

    /**
     * 获取粉丝列表
     * @return 粉丝列表
     */
    @GetMapping("/followers")
    public Result<List<UserFollows>> getFollowersList() {
        Long userId = getUserId(); // 从token中获取当前用户ID
        List<UserFollows> followersList = userFollowsService.getFollowersList(userId);
        return success(followersList);
    }

    /**
     * 获取关注数量
     * @return 关注数量
     */
    @GetMapping("/following/count")
    public Result<Long> getFollowingCount() {
        Long userId = getUserId(); // 从token中获取当前用户ID
        long count = userFollowsService.getFollowingCount(userId);
        return success(count);
    }

    /**
     * 获取粉丝数量
     * @return 粉丝数量
     */
    @GetMapping("/followers/count")
    public Result<Long> getFollowersCount() {
        Long userId = getUserId(); // 从token中获取当前用户ID
        long count = userFollowsService.getFollowersCount(userId);
        return success(count);
    }
}